﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using FarmSmartSleep.Organ.API.Application.Commands.EnterpriseCommands;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.SleepInfoCommands.FemaleBeginCommands
{
    public class SeedmaleListHandler : IRequestHandler<SeedmaleListCommand, Result>
    {
        public readonly IBaseRepository<MaleBegin> maleBeginRepository;//种公
        public readonly IBaseRepository<Cottages> cottagesRepository;//栋舍
        public readonly IBaseRepository<Fields> fieldsRepository;//栏位

        public SeedmaleListHandler(IBaseRepository<MaleBegin> maleBeginRepository, IBaseRepository<Cottages> cottagesRepository, IBaseRepository<Fields> fieldsRepository)
        {
            this.maleBeginRepository = maleBeginRepository;
            this.cottagesRepository = cottagesRepository;
            this.fieldsRepository = fieldsRepository;
        }



        public async Task<Result> Handle(SeedmaleListCommand request, CancellationToken cancellationToken)
        {
            var query = new Result() { Code = (int)CodeEnum.success };
            try
            {
                var lists = (from a in maleBeginRepository.GetAll()
                             join b in cottagesRepository.GetAll()
                             on a.CottagesId equals b.CottagesId
                             join c in fieldsRepository.GetAll()
                             on a.Field equals c.FieIdId
                             select new SeedMaleDto()
                             {
                                 MaleBeginId = a.MaleBeginId,
                                 SleepInforId = a.SleepInforId,
                                 MaleCode= a.MaleCode,
                                 CottagesId = a.CottagesId,
                                 CottageName = b.CottageName,
                                 BirsdayName = a.Field,
                                 MaleData= a.MaleData,
                                 FieIdName = c.FieIdName,
                                 MaleBeginSum = a.MaleBeginSum,
                                 MaleBeginWeight = a.MaleBeginWeight,
                                 CreateId = a.CreateId,
                                 CreateTime = a.CreateTime,
                                 UpdateId = a.UpdateId,
                                 UpdateTime = a.UpdateTime,
                                 DeleteId = a.DeleteId,
                                 Deletes = a.Deletes,
                                 DeleteTime = a.DeleteTime,
                                 ExamId= a.ExamId,
                                 ExamTime = a.ExamTime,
                             }).ToList();
                if (request.SleepInforId != 0)
                {
                    lists = lists.Where(x => x.SleepInforId == request.SleepInforId).ToList();
                }
                query.Code = (int)CodeEnum.success;
                query.Data = lists;
                return query;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
